/**
 * 
 */
package com.core.framework.tools;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.AbstractConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author: LiuTao
 * @since:Aug 20, 2012
 */
public abstract class RequestTools {
	static DateConverter converter = new DateConverter();
	static {
		ConvertUtils.register(converter, java.util.Date.class);

	}
	static Logger log = LoggerFactory.getLogger(RequestTools.class);
	private RequestTools() {
	}
	/**
	 * 从request中得到一个new Bean，request中的字段与返回bean的属性相同 <br />
	 * TODO 需要检验如果request中的属性比bean属性少或者多的情况，是否会有错误。
	 * 
	 * @param <T>
	 * @param beanClass
	 * @param req
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getNewBeanFromRequestSafe(Class<T> beanClass,
			HttpServletRequest req) {
		Object bean = null;
		try {
			bean = beanClass.newInstance();
		} catch (Exception e) {
			log.warn("get Bean from RequestSafe error, entityClass cannot instance.",e);
			return null;
		}
		// 把请求中的参数取出
		Map<?, ?> paramMap = req.getParameterMap();
		try {
			BeanUtils.populate(bean, paramMap);
		} catch (Exception e) {
			log.warn("populate error.", e);
		}
		return (T) bean;
	}
	/**
	 * 从request中覆盖以一个Bean，request中的字段与需要被覆盖的bean的属性相同 <br />
	 * 通常用于 表单修改（先查询出现在的bean，然后根据request中字段覆盖） TODO
	 * 需要检验如果request中的属性比bean属性少或者多的情况，是否会有错误。
	 * 
	 * @param <T>
	 * @param beanClass
	 * @param req
	 * @return
	 */
	public static void overrideBeanFromRequestSafe(Object bean,
			HttpServletRequest req) {
		Map<?, ?> paramMap = req.getParameterMap();
		try {
			ConvertUtils.register(new DateConverter(), java.util.Date.class);
			BeanUtils.populate(bean, paramMap);
		} catch (Exception e) {
			log.warn("populate error.", e);
		}
	}
	/**
	 * @param args
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */

	static class DateConverter extends AbstractConverter {
		Object toDate(Class<?> type, String value) {
			return new WebDate(value);
		}

		@Override
		protected Class<?> getDefaultType() {
			// 
			return Date.class;
		}

		@Override
		protected Object convertToType(Class arg0, Object arg1)
				throws Throwable {

			return toDate(arg0, arg1 + "");
		}
	}
	public static void request2Bean(Object bean,HttpServletRequest request)
	{
		 overrideBeanFromRequestSafe(bean,request);
	}
	public static void main(String[] args) {
//		AAA bean = new AAA();
//		Map map = new HashMap();
//
//		try {
//			map.put("a001", new Object[] { "2011-01-01 12:12:12" });
//			ConvertUtils.register(converter, java.util.Date.class);
//			BeanUtils.populate(bean, map);
//			System.out.println(bean.getA001());
//		} catch (IllegalAccessException e) {
//			// 
//			e.printStackTrace();
//		} catch (Exception e) {
//			// 
//			e.printStackTrace();
//		}
	}

}
